import pygame
import easygui as e
import time as t
import _thread
import random as r
#创建窗口
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('DeadWay')
screen.fill((255, 255, 255))
pygame.display.flip()

#玩家变量
health = 100
dollar = 20
speed = 0.2
player_x = 10
player_y = 499
bag = {"Pistol":[1, 15]}

#游戏变量
running = True
bullet = []  # = [[x, y, damage]]

#普通僵尸
def common():
    global health
    global bullet
    speed = 0.2
    damage = 10
    health = 40
    alive = True
    x = r.randint(400, 600)
    y = 499
    to_damage = False
    while alive:
        #movement
        if x > player_x:
            x -= speed
        if x < player_x:
            x += speed
        #be hurt by bullet
        for i in bullet:
            #判断是否被击中
            if i[0] > x and i[0] < x + 50 and i[1] > y and i[1] < y + 100:
                health -= i[2]
                if health <= 0:
                    alive = False
        #damage
        if round(x) == round(player_x):
            to_damage = True
            while to_damage:
                if round(x) != round(player_x):
                    to_damage = False
                health -= damage
                t.sleep(0.1)
        #draw
        pygame.draw.rect(screen, (255, 0, 0), (x, y, 50, 100))

            
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.USEREVENT:
            running = False

    #获取键盘
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_a]:
        player_x -= speed
    if keys_pressed[pygame.K_d]:
        player_x += speed
    #边界
    if player_x < 0:
        player_x += 1

    #draw
    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 0, 255), (player_x, player_y, 50, 100))

    pygame.display.flip()
    
        